﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

using ClassesTableAdapters;

/// <summary>
/// Summary description for MyClassesBLL
/// </summary>
/// 

[System.ComponentModel.DataObject]
public class ClassBLL
{
    ILE_Global ileGlobal = new ILE_Global();


    private CoursesTableAdapter _coursesAdapter = null;
    protected CoursesTableAdapter CoursesAdapter
    {
        get
        {
            if (_coursesAdapter == null)
                _coursesAdapter = new CoursesTableAdapter();

            return _coursesAdapter;
        }
    }

    private Courses_SectionTableAdapter _coursesSectionAdapter = null;
    protected Courses_SectionTableAdapter CoursesSectionsAdapter
    {
        get
        {
            if (_coursesSectionAdapter == null)
                _coursesSectionAdapter = new Courses_SectionTableAdapter();

            return _coursesSectionAdapter;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Classes.CoursesDataTable GetCourses()
    {
        return CoursesAdapter.GetCourses();
    }

    [System.ComponentModel.DataObjectMethodAttribute
    (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Classes.CoursesDataTable GetCoursesOrderByTitle()
    {
        return CoursesAdapter.GetCoursesOrderByTitle();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Classes.CoursesRow GetCourse(int CourseID)
    {
        return CoursesAdapter.GetCourseByCourseID(CourseID)[0];
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Classes.Courses_SectionDataTable GetSectionsByCourseID(int CourseID, bool GetEnabled)
    {
        try
        {
            if (GetEnabled == false)
            {
                return CoursesSectionsAdapter.GetSectionsByCourseID(CourseID);
            }
            else
            {
                return CoursesSectionsAdapter.GetSectionsEnabledByCourseID(CourseID);
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:GetSection:001", ex, 4);
            return null;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Classes.Courses_SectionRow GetSection(int CourseID, int SectionNumber)
    {
        try
        {
            return CoursesSectionsAdapter.GetSection(CourseID, SectionNumber)[0];
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:GetSection:001", ex, 4);
            return null;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, true)]
    public int InsertCourse(string Title, string Subtitle, string Introduction, bool IsEnabled)
    {
        int result = 0;
        try
        {
            result = Convert.ToInt32(CoursesAdapter.InsertCourse(Title, Subtitle, Introduction, IsEnabled));
            if (result == 0)
            {
                ileGlobal.RecordError("ClassBLL.cs:InsertCourse:001", "Could not insert a row", 3);
                return 0;
            }
            else
            {
                return result;
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:InsertCourse:002", ex, 3);
            return 0;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, true)]
    public bool InsertCourseSection(int courseID, int sectionNumber, bool useSectionInfo, string subtitle, string introduction, string classTime, bool isEnabled)
    {
        bool result = false;
        try
        {
            result = CoursesSectionsAdapter.InsertCourseSection(courseID, sectionNumber, useSectionInfo, subtitle, introduction, classTime, isEnabled) != 0;
            if (result == false)
            {
                ileGlobal.RecordError("ClassBLL.cs:InsertCourseSection:001", "Could not insert a row", 3);
                return false;
            }
            else
            {
                return result;
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:InsertCourseSection:002", ex, 3);
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, true)]
    public bool DeleteCourse(int courseID)
    {
        bool result = false;
        try
        {
            result = CoursesAdapter.Delete(courseID) != 0;
            if (result == false)
            {
                ileGlobal.RecordError("ClassBLL.cs:DeleteCourse:001", "Could not delete a row", 3);
                return false;
            }
            else
            {
                return result;
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:DeleteCourse:002", ex, 3);
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Delete, false)]
    public bool DeleteSection(int courseID, int sectionNumber)
    {
        bool result = false;
        try
        {
            result = CoursesSectionsAdapter.DeleteSection(courseID, sectionNumber) != 0;
            if (result == false)
            {
                ileGlobal.RecordError("ClassBLL.cs:DeleteSection:001", "Could not delete a row", 3);
                return false;
            }
            else
            {
                return result;
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:DeleteSection:002", ex, 3);
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, true)]
    public bool UpdateCourse(string Title, string Subtitle, string Introduction, bool IsEnabled, int CourseID)
    {
        try
        {
            return CoursesAdapter.UpdateCourse(Title, Subtitle, Introduction, IsEnabled, CourseID) == 1;
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:UpdateCourse:001", ex, 3);
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Update, false)]
    public bool UpdateSection(bool UseSectionInfo, string Subtitle, string Introduction, string ClassTime, bool IsEnabled, int CourseID, int SectionNumber)
    {
        try
        {
            return CoursesSectionsAdapter.UpdateCourseSection(UseSectionInfo, Subtitle, Introduction, ClassTime, IsEnabled, CourseID, SectionNumber) == 1;
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("ClassBLL.cs:UpdateSection:001", ex, 3);
            return false;
        }
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Classes.InstructorClassesDataTable GetInstructorClasses(int UserID)
    {
        Classes.InstructorClassesDataTable result = new Classes.InstructorClassesDataTable();

        UsersBLL usersLogic = new UsersBLL();
        Users._Relations_Course_UserDataTable relations = usersLogic.GetCourseUserRelationshipsByUserIdAndType(UserID, "Instructor");

        foreach (Users._Relations_Course_UserRow relation in relations)
        {
            int CourseID = relation.CourseID;
            string CourseTitle = GetCourse(relation.CourseID).Title;
            int SectionNumber = relation.SectionNumber;
            string ClassTime = GetSection(CourseID, relation.SectionNumber).ClassTime;
            bool IsEnabled = false;
            if (GetCourse(relation.CourseID).IsEnabled == true && GetSection(CourseID, SectionNumber).IsEnabled != false)
            {
                IsEnabled = true;
            }

            result.Rows.Add(CourseID, SectionNumber, CourseTitle, ClassTime, IsEnabled);
        }

        return result;

    }
}